<?php
namespace app\api\controller;
use think\facade\Db;
class Form extends Base{

    public function list_data(){
        $param = input('');
        $search_sort = $param['prop'] ? [$param['prop'] => $param['sort']]:[];//排序

        $listData = Db::table('an_form')->order($search_sort)->select()->toArray(); 
        foreach($listData as $k=>$v){
            $listData[$k]['other'] = json_decode($v['other']);
        }
        $this->info['data'] = [
            'list'=>$listData,
            'type'=>[]
        ];
        $this->info['total'] = Db::table('an_form')->count();
        return json($this->info);
    }

    //删除数据
    public function del_data(){
        $param = input('');
        foreach($param['ids'] as $v){
            Db::table('an_form')->where('id',$v)->delete();
        }
        $this->logData('删除表单，id为'.implode(",", $param['ids']));
        return json($this->info);
    } 

    //添加
    public function add_data(){
        $param = input('');
        Db::table('an_form')->insert([
            'create_time'=>time(),
            'name'=>isset($param['name'])?$param['name']:'',
            'phone'=>isset($param['phone'])?$param['phone']:'',
            'email'=>isset($param['email'])?$param['email']:'',
            'age'=>isset($param['age'])?$param['age']:'',
            'sex'=>isset($param['sex'])?$param['sex']:'',
            'notes'=>isset($param['notes'])?$param['notes']:'',
            'client'=>isset($param['client'])?$param['client']:'',
            'addr'=>isset($param['addr'])?$param['addr']:'',
            'other'=>isset($param['other'])?json_encode($param['other']):'',
        ]);
        return json($this->info);
    }
}
